// pages/title/title.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items:{},
    id:"0",//当前题目id
    answer: ["cuo", "cuo", "cuo", "cuo"],//当前答案
    state: ["none", "none", "none", "none"],
    analyze: "none",
    laststate:"",
    touch:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let apiname=this.apichange(options.id);
    wx.request({
      url: `${app.globalData.apiurl}${apiname}/list`,
      method:"GET",
      data:{
        account:10
      },
      success:res=>{
        console.log(res)
        let count=res.data.length
        for(let i=0;i<count;i++){
          if (res.data[i].answer=="A"){
            res.data[i].answer = ["dui", "cuo", "cuo", "cuo","cuo"]
          }
          if (res.data[i].answer == "B") {
            res.data[i].answer = ["cuo", "dui", "cuo", "cuo", "cuo"]
          }
          if (res.data[i].answer == "C") {
            res.data[i].answer = ["cuo", "cuo", "dui", "cuo", "cuo"]
          }
          if (res.data[i].answer == "D") {
            res.data[i].answer = ["cuo", "cuo", "cuo", "dui", "cuo"]
          }
        }
        this.setData({
          items: res.data,
          answer: res.data[this.data.id].answer
        })
        console.log(this.data.items[this.data.id])
      }
    })
  },
  //下一题点击事件
  bindNext: function (e) {
    let state=this.data.state;
    state = ["none", "none", "none", "none"];
    this.setData({
      state:state,
      analyze: "none",
      laststate: "",
      touch:0
    })
    if(this.data.id==9){
      wx.showToast({
        title: '已经做完10题！点左上角返回！',
        icon:"none"
      })
      return false;
    }else{
      let ide=this.data.id*1+1
      console.log(ide)
      this.setData({
        id:ide,
        answer:this.data.items[ide].answer
      })
      console.log(this.data.items[this.data.id])
    }
  },
  //上一题点击事件
  bindLast:function(e){
    let state = this.data.state;
    state = ["none", "none", "none", "none"];
    this.setData({
      state: state,
      analyze: "none",
      touch:0
    })
    if (this.data.id == 0) {
      wx.showToast({
        title: '无上一题！',
        icon:"none"
      })
      return false;
    } else {
      let ide = this.data.id * 1 - 1
      this.setData({
        id: ide,
        answer: this.data.items[ide].answer,
        touch:0
      })
      if(ide==0){
        this.setData({
          laststate: ""
        })
      }
      console.log(this.data.items[this.data.id])
    }
  },
  //选项点击事件
  chance:function(e){
    if(this.data.touch==1){
      return false;
    }
    console.log(this.data.answer[e.currentTarget.id])
    if (this.data.answer[e.currentTarget.id]=="cuo"){
      if (this.data.state[e.currentTarget.id] == "none") {
        let state = this.data.state;
        state = ["","","",""];
        this.setData({
          state: state,
          analyze: ""
        })
      }
    }else{
      let state = this.data.state;
      state[e.currentTarget.id] = "";
      this.setData({
        state: state,
        touch:1
      })
    }
    
  },
  //api名称转换
  apichange:function(id){
    if(id==1){
      let name = "gqbYwOption"
      return name;
    }
    if(id==10){
      let name = "gqbYyPronunciation"
      return name;
    }
    if(id == 20){
      let name = "gqzYyPronunciation"
      return name;
    }
    if (id == 23) {
      let name = "gqzYwOption"
      return name;
    }
    if (id == 26) {
      let name = "gqzYyOption"
      return name;
    }
    if (id == 27) {
      let name = "zsbZzOption"
      return name;
    }
    if (id == 37) {
      let name = "zsbYyPronunciation"
      return name;
    }
    if (id == 38) {
      let name = "zsbYyOption"
      return name;
    }
    if (id == 41) {
      let name = "zsbYxzhDoctora"
      return name;
    }
    if (id == 46) {
      let name = "gqbYyOption"
      return name;
    }
    if (id == 53) {
      let name = "gqbSdOption"
      return name;
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})